package edu.pku.sei.transformation.editor.controller.commands.node;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.transformation.editor.model.constraint.Constraint;
import edu.pku.sei.transformation.editor.model.execution.Model;
import edu.pku.sei.transformation.editor.model.execution.TransformationExecution;
import edu.pku.sei.transformation.editor.model.test.CheckPoint;
import edu.pku.sei.transformation.editor.model.test.TestCase;

public class TestCaseNestedElementDeleteCommand extends Command {
	private TestCase father;
	private MetaModelElement child;

	public TestCaseNestedElementDeleteCommand(MetaModelElement child,
			TestCase father) {
		this.father = father;
		this.child = child;
	}

	@Override
	public boolean canExecute() {
		if (father == null || child == null)
			return false;
		else if (child instanceof Model || child instanceof CheckPoint
				|| child instanceof Constraint)
			return super.canExecute();
		else if (child instanceof TransformationExecution
				&& father.getTestee() != null)
			return super.canExecute();
		else
			return false;
	}

	@Override
	public void execute() {
		if (child instanceof TransformationExecution) {
			if (father.getTestee() == child) {
				father.setTestee(null);
			}
		} else if (child instanceof Model) {
			father.getTestDatas().remove((Model) child);
		} else if (child instanceof CheckPoint) {
			father.getCheckPoints().remove((CheckPoint) child);
		} else if (child instanceof Constraint) {
			father.getConstraints().remove(child);
		}
	}

	public void undo() {
		if (child instanceof TransformationExecution) {
			if (father.getTestee() == null) {
				father.setTestee((TransformationExecution) child);
			}
		} else if (child instanceof Model) {
			father.getTestDatas().add((Model) child);
		} else if (child instanceof CheckPoint) {
			father.getCheckPoints().add((CheckPoint) child);
		} else if (child instanceof Constraint) {
			father.getConstraints().add((Constraint) child);
		}
	}
}
